package drive.RemakeController;


import com.aliyuncs.exceptions.ClientException;
import drive.Model.Article;

import drive.Model.Article_Img;
import drive.RemakeService.ServiceInterface.TravelNoteService;
import drive.Service.ServiceInterface.BaiduService;
import org.codehaus.jettison.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/travel-note")
public class TravelNoteController {

    @Autowired
    private TravelNoteService travelNoteService;

    @Autowired
    private BaiduService baiduService;

    //把图片上传到阿里云的接口，会直接返回阿里云的图片url
    @PostMapping("/upload")
    public String upload(MultipartFile file) throws Exception {
        return baiduService.upload(file);
    }

    //回传识别结果和识别后的图片
    @PostMapping("/image_back")
    public String image_back(MultipartFile file) throws IOException, ClientException {
        String url = baiduService.upload(file);
        String originalFilename = file.getOriginalFilename();
        return travelNoteService.image_back(url,originalFilename);
    }


    @PostMapping("/detect")//前端请求type控制识别动物还是植物，url使用阿里云生成的网络图片
    public String detect(String url,String type){
        System.out.println(type);
        if(type.equals("animal")){
            System.out.println("动物");
            return baiduService.detect(url);
        }else{
            return baiduService.Plant_detect(url);
        }
    }

    @PostMapping("/getkeyword")
    public String getKeyWord(String title,String content) throws JSONException, IOException {
        return baiduService.getKeyWord(title,content);
    }

//    @PostMapping("/publish")
//    public String publishTravelNote(@RequestBody Article article,@RequestParam String img_url) throws JSONException, IOException {
//        return travelNoteService.publishTravelNote(article,img_url);
//    }
    //已废弃接口


    @PostMapping("/publish")
    public String publishTravelNote_test(@RequestBody Article_Img article_img) throws JSONException, IOException {
        return travelNoteService.publishTravelNote(article_img.getArticle(),article_img.getImg_url());
    }

    @GetMapping("/browse")
    public String browseTravelNotes(int articleID) {
        return travelNoteService.browseTravelNotes(articleID);
    }

    @PostMapping("/search")
    public String searchTravelNotes(@RequestParam String keyword,int PageNum,int PageSize) {
        return travelNoteService.searchTravelNotes(keyword,PageNum,PageSize);
    }

    @PostMapping("/report")
    public String reportTravelNote(@RequestHeader("token") String token, @RequestParam Integer articleID, @RequestParam String reason) {
        return travelNoteService.reportTravelNote(token, articleID, reason);
    }

    @PostMapping("/follow-author")
    public String followTravelNoteAuthor(@RequestHeader("token") String token, @RequestParam Integer authorID) {
        return travelNoteService.followTravelNoteAuthor(token,authorID);
    }

    @PostMapping("/comment")
    public String commentTravelNote(@RequestHeader("token") String token, @RequestParam Integer articleID, @RequestParam String comment, int parentCommentID) {
        return travelNoteService.commentTravelNote(token, articleID, comment, parentCommentID);
    }

    @PostMapping("/collect")
    public String collectTravelNote(@RequestHeader("token") String token, @RequestParam Integer articleID) {
        return travelNoteService.collectTravelNote(token,articleID);
    }

    @GetMapping("/firstimage")
    public String getFirstImageByItemID(@RequestParam int articleID) {
        return travelNoteService.getFirstImageByArticleID(articleID);
    }

    @GetMapping("/image")
    public String getImageByItemID(@RequestParam int articleID) {
        return travelNoteService.getImageByArticleID(articleID);
    }

    /**
     * 删除评论
     * @param commentID 评论ID
     * @return 操作结果的JSON字符串
     */
    @DeleteMapping("/delete_comment")
    public String deleteComment(@RequestParam int commentID) {
        return travelNoteService.deleteComment(commentID);
    }

    /**
     * 取消收藏
     * @param favoriteID 收藏记录ID
     * @return 操作结果的JSON字符串
     */
    @DeleteMapping("/delete")
    public String deleteFavorite(@RequestParam int favoriteID) {
        return travelNoteService.deleteFavorite(favoriteID);
    }

    /**
     * 根据用户ID和文章ID获取收藏ID
     * @param userID 用户ID
     * @param articleID 文章ID
     * @return 包含收藏ID的JSON字符串
     */
    @GetMapping("/getByIds")
    public String getFavoriteIdByUserAndArticle(@RequestParam int userID, @RequestParam int articleID) {
        return travelNoteService.getFavoriteIdByUserAndArticle(userID, articleID);
    }

}